package com.sgb.goods.view.activity.details.order;

import android.view.View;

import androidx.core.content.ContextCompat;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.sgb.goods.BR;
import com.sgb.goods.R;
import com.sgb.goods.databinding.GoodsActivityInvoiceDetailsBinding;
import com.sgb.goods.entity.InvoiceDetailsEntity;
import com.sgb.goods.utils.Constants;
import com.sgb.goods.utils.base.BaseActivity;
import com.sgb.goods.viewmodel.InvoiceDetailsViewModel;
import com.swgk.core.recyclerview.decoration.RecyclerCustomDecoration;

import java.util.Objects;

/**
 * 发票信息（采购、销售订单模块）
 *
 * @author cyj
 */
@Route(path = Constants.ARouter.AR_INVOICE_DETAIL)
public class InvoiceDetailsActivity extends BaseActivity<GoodsActivityInvoiceDetailsBinding, InvoiceDetailsViewModel> {

    @Override
    protected int getLayoutResId() {
        return R.layout.goods_activity_invoice_details;
    }

    @Override
    protected void initViewModel() {
        mViewModel = createViewModel(InvoiceDetailsViewModel.class, BR.viewModel);
    }

    @Override
    protected void init() {
        mDataBinding.head.title.setText("发票信息");
        mDataBinding.rlvInvoiceDetails.addItemDecoration(new RecyclerCustomDecoration(10, 10, 0,
                getResources().getDimension(R.dimen.dp1), ContextCompat.getColor(this, R.color.goods_color_FFF5F5F5)));
        int orderIdentity = getIntent().getIntExtra("orderIdentity", 0);
        //1：企业下单 0 个人下单 2 团体下单
        if (orderIdentity == 0) {
            mDataBinding.containerBankCode.setVisibility(View.GONE);
        }
        // 查询发票详情
        mViewModel.getInvoiceDetails(getIntent().getStringExtra("id"));
    }

    @Override
    protected void initListener() {
        mDataBinding.head.ivBack.setOnClickListener(v -> finish());
        mViewModel.adapter.setOnItemChildClickListener((adapter, view, position) -> {
            if (view.getId() == R.id.invoice_operation) {
                InvoiceDetailsEntity.ListDTO entity = (InvoiceDetailsEntity.ListDTO) view.getTag();
                jumpPreview(entity);
            }
        });
    }

    private void jumpPreview(InvoiceDetailsEntity.ListDTO entity) {
        if (null != entity) {
            String url = "https://shigongbang.oss-cn-hangzhou.aliyuncs.com/" + Objects.requireNonNull(entity.fileUrl);
            ARouter.getInstance().build(Constants.ARouter.AR_PREVIEW)
                    .withString("url", url.replaceAll("\"", ""))
                    .withString("fileUrl", Objects.requireNonNull(entity.fileUrl.replaceAll("\"", "")))
                    .navigation();
        }
    }
}